package com.example.adminvue.controller;

import com.example.adminvue.config.ParamConfig;
import com.example.adminvue.domain.AjaxResult;
import com.example.adminvue.domain.LoginUser;
import com.example.adminvue.domain.SysDept;
import com.example.adminvue.domain.User;
import com.example.adminvue.service.ISysUserService;
import com.example.adminvue.service.SysDeptService;
import com.example.adminvue.utils.FileUploadUtils;
import com.example.adminvue.utils.SecurityUtils;
import com.example.adminvue.utils.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 个人信息 业务处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/user/profile")
public class SysProfileController extends BaseController
{
    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysDeptService deptService;

    /**
     * 个人信息
     */
    @GetMapping
    public AjaxResult profile()
    {
        LoginUser loginUser = getLoginUser();
        User user = loginUser.getUser();
        SysDept dept = deptService.selectDeptById(user.getDeptId());
        user.setDeptName(dept.getDeptName());
        AjaxResult ajax = AjaxResult.success(user);
        ajax.put("roleGroup", userService.selectUserRoleGroup(loginUser.getUsername()));
        ajax.put("postGroup", userService.selectUserPostGroup(loginUser.getUsername()));
        return ajax;
    }

    /**
     * 修改用户
     */
    @PutMapping
    public AjaxResult updateProfile(@RequestBody User user)
    {
        LoginUser loginUser = getLoginUser();
        User sysUser = loginUser.getUser();
        user.setUserName(sysUser.getUserName());
        user.setUserId(sysUser.getUserId());
        user.setPassword(null);
        int r = userService.updateUserProfile(user);
        // 更新缓存用户信息
        if(r>0){
            userService.updateCache(user);
        }
        return AjaxResult.success();
    }

    /**
     * 重置密码
     */
    @PutMapping("/updatePwd")
    public AjaxResult updatePwd(String oldPassword, String newPassword)
    {
        LoginUser loginUser = getLoginUser();
        String userName = loginUser.getUsername();
        String password = loginUser.getPassword();
        if (!SecurityUtils.matchesPassword(oldPassword, password))
        {
            return AjaxResult.error("修改密码失败，旧密码错误");
        }
        if (SecurityUtils.matchesPassword(newPassword, password))
        {
            return AjaxResult.error("新密码不能与旧密码相同");
        }
        userService.resetUserPwd(userName, SecurityUtils.encryptPassword(newPassword));
        return AjaxResult.success();
    }

    /**
     * 头像上传
     */
//    @PostMapping("/avatar")
//    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
//    {
//        if (!file.isEmpty())
//        {
//            LoginUser loginUser = getLoginUser();
//            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
//            if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
//            {
//                AjaxResult ajax = AjaxResult.success();
//                ajax.put("imgUrl", avatar);
//                // 更新缓存用户头像
//                loginUser.getUser().setAvatar(avatar);
//                tokenService.setLoginUser(loginUser);
//                return ajax;
//            }
//        }
//        return AjaxResult.error("上传图片异常，请联系管理员");
//    }

    @PostMapping("upload")
    public AjaxResult upload(MultipartFile file)
    {
        AjaxResult ajax= AjaxResult.success();
        try{
            if (!file.isEmpty())
            {
                String headpic = FileUploadUtils.upload(ParamConfig.getUploadPath(), file);
                if (!StringUtils.isEmpty(headpic))
                {
                    LoginUser loginUser = getLoginUser();
                    User user = loginUser.getUser();
                    user.setAvatar(headpic);
                    userService.updateUserAvatar(loginUser.getUsername(),headpic);
                    ajax.put("goodsImg", headpic);
                    // 更新缓存用户头像
                    userService.updateCache(user);
                    return ajax;
                }
                else{
                    ajax= AjaxResult.error("上传失败");
                }
            }
            else{
                ajax= AjaxResult.error("文件为空");
            }
        }
        catch (Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
        return ajax;
    }

}
